#    Rubatom
#    Copyright (c) 2014 Sebastian Dufner
#
#    This file is part of Rubatom.
#
#    Rubatom is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Rubatom is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

##
#

module Rubatom
  class Structure
    class Block
      def self.deserialize(input)
        new(*input.read_many(1, 1, 1))
      end

      def initialize(b1, b2, b3)
        @block = (b1 << 16) | (b2 << 8) | b3
      end
      attr_reader :block

      def id
        @block & 0x7ff
      end

      def id=(newid)
        @block = (@block >> 11 << 11) | (newid & 0x7ff)
      end

      def hp
        (@block >> 11) & 0xff
      end

      def hp=(newhp)
        @block = (@block >> 19 << 19) | (@block & 0x7ff) | (newhp & 0xff) << 11
      end

      def serialize(output)
        output.write_many(
              [
                (block >> 16 & 0xff),
                (block >> 8 & 0xff),
                (block & 0xff)
              ] => [1, 1, 1])
      end
    end

  end
end
